package com.yy.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.IdUtil;


import com.yy.common.annotation.Log;
import com.yy.common.constant.PmsConstants;
import com.yy.common.core.controller.BaseController;
import com.yy.common.core.domain.AjaxResult;
import com.yy.common.core.page.TableDataInfo;
import com.yy.common.enums.BusinessType;
import com.yy.common.utils.poi.ExcelUtil;
import com.yy.domain.PayTemp;
import com.yy.service.IPayTempService;
import com.yy.system.service.ISysConfigService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;






/**
 * 临时收费Controller
 * 
 * @author yy
 * @date 2023-04-11
 */
@RestController
@RequestMapping("/pay/temp")
public class PayTempController extends BaseController
{
    @Autowired
    private IPayTempService payTempService;

    @Autowired
    private ISysConfigService configService;
    /**
     * 导出临时收费列表
     */
    @PreAuthorize("@ss.hasPermi('pay:temp:export')")
    @Log(title = "临时收费", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PayTemp payTemp)
    {
        List<PayTemp> list = payTempService.selectPayTempList(payTemp);
        ExcelUtil<PayTemp> util = new ExcelUtil<PayTemp>(PayTemp.class);
        util.exportExcel(response, list, "临时收费数据");
    }
    /**
     * 查询临时收费列表
     */
    @PreAuthorize("@ss.hasPermi('pay:temp:list')")
    @GetMapping("/list")
    public TableDataInfo list(PayTemp payTemp)
    {
        startPage();
        List<PayTemp> list = payTempService.selectPayTempList(payTemp);
        return getDataTable(list);
    }
    /**
     * 获取临时收费详细信息
     */
    @PreAuthorize("@ss.hasPermi('pay:temp:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(payTempService.selectPayTempById(id));
    }

    /**
     * 新增临时收费
     */
    @PreAuthorize("@ss.hasPermi('pay:temp:add')")
    @Log(title = "临时收费", businessType = BusinessType.INSERT)
    @PostMapping("addTemp")
    public AjaxResult addTemp(@RequestBody PayTemp payTemp)
    {
        //设置状态
        payTemp.setState(PmsConstants.PAY_ORDER_STATE_0);
        //设置创建人
        payTemp.setCreateBy(getUsername());
        //设置创建时间
        payTemp.setCreateTime(new Date());
        //生成支付单号
        String prefix = configService.selectConfigByKey(PmsConstants.PREFIX_PAY_TEMP_LS);
        payTemp.setPayNo(prefix + IdUtil.getSnowflakeNextIdStr());
        return toAjax(payTempService.insertPayTemp(payTemp));
    }

    /**
     * 临时收费退费
     */
    @PreAuthorize("@ss.hasPermi('pay:deposit:refund')")
    @Log(title = "临时收费退费", businessType = BusinessType.UPDATE)
    @PostMapping("refundTemp")
    public AjaxResult refundTemp(@RequestBody PayTemp payTemp) {
        //取出退费方式
        String refundType = payTemp.getRefundType();
        String remark = payTemp.getRemark();
        //根据ID查询订单数据
        PayTemp temp = this.payTempService.selectPayTempById(payTemp.getId());
        if (null == temp) {
            return AjaxResult.error("当前订单号不存在");
        }
        if (!temp.getState().equals(PmsConstants.PAY_ORDER_STATE_2)) {
            return AjaxResult.error("当前订单状态不是支付成功状态，不能进行退费操作");
        }
        temp.setRefundType(refundType);
        temp.setRemark(remark);
        temp.setState(PmsConstants.PAY_ORDER_STATE_5);
        //设置退款人和退款时间
        temp.setRefundUser(getUsername());
        temp.setRefundTime(new Date());
        String prefix = configService.selectConfigByKey(PmsConstants.PREFIX_REFUND_TEMP_LS);
        temp.setRefundNo(prefix + IdUtil.getSnowflakeNextIdStr());
        return toAjax(payTempService.updatePayTemp(temp));
    }
    /**
     * 确认收费
     */
    @PreAuthorize("@ss.hasPermi('pay:deposit:confirmPay')")
    @Log(title = "确认收费", businessType = BusinessType.INSERT)
    @PostMapping("confirmPay/{id}")
    public AjaxResult confirmPay(@PathVariable Long id) {
        //根据id查询订单数据
        PayTemp temp = this.payTempService.selectPayTempById(id);
        if (null == temp) {
            return AjaxResult.error("当前订单号不存在");
        }
        if (!temp.getState().equals(PmsConstants.PAY_ORDER_STATE_0)) {
            return AjaxResult.error("当前订单状态不是创建状态，不能确认收费");
        }
        //设置收款人
        temp.setOperateUser(getUsername());
        //设置收款时间
        temp.setOperateTime(new Date());
        //更新
        temp.setState(PmsConstants.PAY_ORDER_STATE_2);
        return toAjax(this.payTempService.updatePayTemp(temp));
    }

}
